package com.taoyi.mhouse.binder;
/**
 * created by tempo on 2015/11/13.
 */

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taoy.recyclebinder.DataBindAdapter;
import com.taoy.recyclebinder.DataBinder;
import com.taoyi.mhouse.R;
import com.taoyi.mhouse.bean.Goods;
import com.taoyi.mhouse.util.UIUtils;
import com.tempo.widget.AutoLineLayout;

import java.util.ArrayList;
import java.util.List;

public class SixAnimeBinder extends DataBinder<SixAnimeBinder.ViewHolder> {
    private int itemCount=5;
    public SixAnimeBinder(DataBindAdapter dataBindAdapter) {
        super(dataBindAdapter);
    }
    @Override
    public ViewHolder newViewHolder(ViewGroup parent) {
        View v = View.inflate(UIUtils.getContext(),R.layout.binder_six_anime, null);
        return new ViewHolder(v);
    }

    /**
     * 测试用
     * @param count
     */
    public void setItemCount(int count){
        itemCount=count;
    }
    @Override
    public void bindViewHolder(ViewHolder holder, int position) {
        List<Goods>data=new ArrayList<Goods>();
        for(int i=0;i<6;i++){
            Goods goods=new Goods();
            goods.imgUrl = "";
            goods.name="灌篮高手"+i;
            goods.price=100;
            data.add(goods);
        }
       holder.setData(data);
    }
    @Override
    public int getItemCount() {
        return itemCount;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public AutoLineLayout alGoods;
        private AutoAdapter  mAdapter;
        public ViewHolder(View itemView) {
            super(itemView);
            alGoods = (AutoLineLayout) itemView.findViewById(R.id.al_animes);
            mAdapter=new AutoAdapter();
            alGoods.setAdapter(mAdapter);
        }
        public void setData(List<Goods> data){
          mAdapter.setData(data);
        }

    }
    static class AutoAdapter extends AutoLineLayout.AutoAdapter{
        private List<Goods> data;
        private List<Integer> imgs;
        public void setData(List<Goods> data){
            this.data=data;
            imgs=new ArrayList<>();
            imgs.add(R.drawable.toy1);
            imgs.add(R.drawable.toy2);
            imgs.add(R.drawable.toy3);
            imgs.add(R.drawable.toy4);
            notifyDataChaged();

        }
        @Override
        protected int getCount() {
            return data==null?0:data.size();
        }

        @Override
        protected Goods getData(int position) {
            return data.get(position);
        }

        @Override
        protected View getView(View parent, int position) {
            View v=View.inflate(UIUtils.getContext(),R.layout.item_six_anime,null);
            Holder holder=new Holder();
            holder.ivAnime= (ImageView) v.findViewById(R.id.iv_anime);
            holder.tvAnimeName= (TextView) v.findViewById(R.id.tv_anime);
            v.setTag(holder);
            return v;
        }

        @Override
        protected void resetData(View child, int position) {
            Holder holder= (Holder) child.getTag();
            Goods goodsData=getData(position);
            holder.ivAnime.setImageResource(R.drawable.c);
            holder.tvAnimeName.setText(goodsData.name);


        }
        private class Holder{
            public ImageView ivAnime;
            public TextView tvAnimeName;
        }
    }
}
